package com.servlet.mysert;

import com.domain.MeetingRecord;
import com.domain.Mysert;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.MysertService;
import com.service.impl.MysertServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/mysertSelectByDateServlet")
public class MysertSelectByDateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String date = request.getParameter("date");
        MysertService service = new MysertServiceImpl();
        ArrayList<Mysert> myserts = service.selectByDate(date);
        if(myserts == null || myserts.size() < 1) {
            response.getWriter().write("{\"status\":\"0\",\"message\":\"ok\",\"data\":[]}");
            return;
        }
        ObjectMapper mapper = new ObjectMapper();
        StringBuilder str = new StringBuilder();
        for(Mysert mysert : myserts){
            String j = mapper.writeValueAsString(mysert);
            str.append(j);
            str.append(",");
        }
        str.deleteCharAt(str.length()-1);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        response.getWriter().write("{\"status\":\"0\",\"message\":\"ok\",\"data\":["+str+"]}");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
